/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mycompany.controller.account;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.web.controller.account.BroadleafUpdateAccountController;
import org.broadleafcommerce.profile.core.service.CustomerService;
import org.broadleafcommerce.profile.web.core.CustomerState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mycompany.core.profile.domain.HCCustomerImpl;

@Controller
@RequestMapping("/account")
public class UpdateAccountController extends BroadleafUpdateAccountController {

	@Resource(name="loginUpdationValidator")
	CustomizedLoginUpdationValidation customizedLoginUpdationValidation;

	@Resource(name = "blCustomerService")
	protected CustomerService customerService;
	/*login_post.htm
	@Resource(name="loginUpdation")
	CustomizedLoginUpdation customizedLoginUpdation;*/

	@RequestMapping(method = RequestMethod.GET)
	public String viewUpdateAccount(HttpServletRequest request, Model model, @ModelAttribute("customizedFieldLoginUpdation") CustomizedFieldLoginUpdation form) {

		
		HCCustomerImpl customer = (HCCustomerImpl)CustomerState.getCustomer();
		form.setEmailAddress(customer.getEmailAddress());
		form.setFirstName(customer.getFirstName());
		form.setLastName(customer.getLastName());
		form.setSecurityQuestion(customer.getSecurityQuestion());
		form.setSecurityAnswer(customer.getSecurityAnswer());
		return getUpdateAccountView();

		//return super.viewUpdateAccount(request, model, form);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processUpdateAccount(HttpServletRequest request, Model model, @ModelAttribute("customizedFieldLoginUpdation") CustomizedFieldLoginUpdation form, BindingResult result, RedirectAttributes redirectAttributes) throws ServiceException {
		customizedLoginUpdationValidation.validate(form, result);
		if (result.hasErrors()) {
			return getUpdateAccountView();
		}
		HCCustomerImpl customer1 = (HCCustomerImpl)CustomerState.getCustomer();
		customer1.setEmailAddress(form.getEmailAddress());
		customer1.setFirstName(form.getFirstName());
	customer1.setLastName(form.getLastName());
		customer1.setSecurityQuestion(form.getSecurityQuestion());
		customer1.setSecurityAnswer(form.getSecurityAnswer());
		
		/*customer1.getCustomerAttributes().add((CustomerAttribute) securityQuestionAttr);*/
		//customizedFieldLoginUpdation.setSecurityAnswer()
		customer1=(HCCustomerImpl) customerService.saveCustomer(customer1);
		CustomerState.setCustomer(customer1);
		redirectAttributes.addFlashAttribute("successMessage", getAccountUpdatedMessage());
		return getAccountRedirectView();  

	//	return super.processUpdateAccount(request, model, form, result, redirectAttributes);
	}



}
